###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/RemoveResources.pm.pl -> RemoveResources module                #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::RemoveResources;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "ControlPanel/Output/RemoveResources.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "RESOURCES_REMOVE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my @RID = $SL::CGI->param('RID');

  if (scalar(@RID) < 1) {
    &Error::Error("CP", MESSAGE => "You didn't specify any Resource IDs (RID)");
  }

  my ($Resources, $ResourcesIndex) = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => {
      ID  => \@RID
    },
    MATCH   => "ANY",
    SORT    => "TITLE",
    BY      => "A-Z"
  );

  my @Categories;
  my %CategoriesCount;
  foreach my $resource (@{ $Resources }) {
    push(@Categories, $resource->{'CATEGORY'});
    $CategoriesCount{$resource->{'CATEGORY'}}++ if ($resource->{'STATUS'});
  }
  
  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    WHERE   => {
      ID  => \@Categories
    },
    MATCH   => "ANY"
  );

  &Authenticate::CheckPermission(ACTION => "RESOURCES_REMOVE", CATEGORIES => \@Categories);

  #----------------------------------------------------------------------#
  # Deleting data...                                                     #

  (
    $in{'DB'}->Delete(
      TABLE   => "Resources",
      KEYS    => \@RID
    )
  ) || &Error::Error("CP", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
  
  foreach my $resource (@{ $Resources }) {
    next unless ($resource->{'UPLOAD_FILENAME'} && -e "$SYSTEM->{'PUBLIC_PATH'}/Upload/$resource->{'UPLOAD_FILENAME'}");
    unlink("$SYSTEM->{'PUBLIC_PATH'}/Upload/$resource->{'UPLOAD_FILENAME'}")
      || &Error::CGIError("Error deleting image upload file. $!", "$SYSTEM->{'PUBLIC_PATH'}/Upload/$resource->{'UPLOAD_FILENAME'}");
  }
  
  foreach my $key (keys %CategoriesCount) {
    (
      $in{'DB'}->Update(
        TABLE   => "Categories",
        VALUES  => {
          RESOURCES => ($Categories->[$CategoriesIndex->{$key}]->{'RESOURCES'} - $CategoriesCount{$key})
        },
        KEY     => $key
      )
    ) || &Error::Error("CP", MESSAGE => "Error updating record. $in{'DB'}->{'ERROR'}");
  }

  my ($Rates, $RatesIndex) = $in{'DB'}->Query(
    TABLE   => "Rates",
    WHERE   => {
      RID => \@RID
    },
    MATCH   => "ANY"
  );
  my @RatID;
  foreach my $rate (@{ $Rates }) {
    push(@RatID, $rate->{'ID'});
  }

  if (scalar(@RatID) >= 1) {
    (
      $in{'DB'}->Delete(
        TABLE   => "Rates",
        KEYS    => \@RatID
      )
    ) || &Error::Error("CP", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
  }

  my ($Reviews, $ReviewsIndex) = $in{'DB'}->Query(
    TABLE   => "Reviews",
    WHERE   => {
      RID => \@RID
    },
    MATCH   => "ANY"
  );
  my @RevID;
  foreach my $review (@{ $Reviews }) {
    push(@RevID, $review->{'ID'});
  }

  if (scalar(@RevID) >= 1) {  
    (
      $in{'DB'}->Delete(
        TABLE   => "Reviews",
        KEYS    => \@RevID
      )
    ) || &Error::Error("CP", MESSAGE => "Error deleting records. $in{'DB'}->{'ERROR'}");
  }

  my $StaticPages = StaticPages->new(DB => $in{'DB'});
  
  foreach my $resource (@{ $Resources }) {
    $StaticPages->Update(TYPE => "Detail", INPUT => $resource)
      || &Error::Error("CP", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");
  }
  
  $StaticPages->Remove(TYPE => "Detail", KEYS => \@RID)
    || &Error::Error("CP", MESSAGE => "Error updating static pages. $StaticPages->{'ERROR'}");

  my %INPUT;
  
  $INPUT{'RESOURCES'} = $Resources;
  $INPUT{'CATEGORIES'} = $Categories;
  $INPUT{'CATEGORIES_IX'} = $CategoriesIndex;
  
  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::RemoveResources->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;